import unittest

import requests

from api.openAccountApi import OpenAccountApi
from api.regLoginApi import RegLoginApi
from common import utils
from common.utils import html_util


class TestOpenAccount(unittest.TestCase):

    def setUp(self) -> None:
        self.session = requests.session()
        self.open_account_api = OpenAccountApi(self.session)
        self.reg_login_api = RegLoginApi(self.session)

    def tearDown(self) -> None:
        # 关闭session对象
        self.session.close()

    # 实名认证测试用例
    def test01_realname(self):
        self.reg_login_api.user_login('15814662599', '123456a')
        resp = self.open_account_api.realname("张三", "632323190605260500")
        data_json = resp.json()
        print(f'实名认证:{data_json}')
        # 公共断言
        utils.common_assert(self, resp, 200, '成功')
        self.assertIn('632****500', data_json.get('data').get('card_id'))
        self.assertIn('张*', data_json.get('data').get('realname'))

    # 开户测试用例
    def test02_open_account(self):
        # 实现登录接口
        self.reg_login_api.user_login('15814662599', '123456a')
        # 实现开户接口
        resp = self.open_account_api.open_account()
        data_json = resp.json()
        print(f'实名认证:{data_json}')
        # 断言
        utils.common_assert(self, resp, 200, 'form')

    # 第三方开户测试用例
    def test03_third_open_account(self):
        # 实现登录接口
        self.reg_login_api.user_login('15814662599', '123456a')
        # 实现开户接口
        resp = self.open_account_api.open_account()
        # 实现第三方开户接口
        url = html_util(resp)[0]
        info = html_util(resp)[1]
        resp = self.open_account_api.third_open_account(url, info)
        data_json = resp.text
        print(data_json)
        # 断言
        self.assertEqual(200, resp.status_code)
        self.assertIn('OK', data_json)